package org.zp.util;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class Ajax {

	public static void error(HttpServletResponse resp,String errmsg,int errcode) {
		HashMap<String, Object>map = new HashMap<String, Object>();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		try {
			resp.getWriter().println(JSONObject.toJSON(map));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return;
	}
	
	public static void error(HttpServletResponse resp,String errmsg) {
		HashMap<String, Object>map = new HashMap<String, Object>();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		map.put("errmsg", errmsg);
		try {
			resp.getWriter().println(JSONObject.toJSON(map));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return;
	}
	public static void error(HttpServletResponse resp,int errcode) {
		HashMap<String, Object>map = new HashMap<String, Object>();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		map.put("errcode", errcode);
		try {
			resp.getWriter().println(JSONObject.toJSON(map));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return;
	}
	
	
	public static void success(HttpServletResponse resp) {
		HashMap<String, Object>map = new HashMap<String, Object>();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		map.put("success", true);
		try {
			resp.getWriter().println(JSONObject.toJSON(map));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return;
	}
	
	public static void success(HttpServletResponse resp,String key ,Object value) {
		HashMap<String, Object>map = new HashMap<String, Object>();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		map.put("success", true);
		
		if(null !=key && null != value) {
			map.put(key, value);
		}
		
		try {
			resp.getWriter().println(JSONObject.toJSON(map));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return;
	}
	
	public static void success(HttpServletResponse resp,String key1 ,String key2 ,Object value1,Object value2) {
		HashMap<String, Object>map = new HashMap<String, Object>();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		map.put("success", true);
		
		if(null !=key1 && null != value1) {
			map.put(key1, value1);
		}
		if(null !=key2 && null != value2) {
			map.put(key2, value2);
		}
		
		try {
			resp.getWriter().println(JSONObject.toJSON(map));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return;
	}
}
